home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- _pre_functions = []
- _post_functions = []
-
- def register_pre_handler(pred, pre):
- _pre_functions.insert(0, (pred, pre))
-
-
- def register_post_handler(pred, post):
- _post_functions.insert(0, (pred, post))
-
-
- def register_prompt_handler(f):
- funcs = f()
- pred = funcs.get('pred')
- pre = funcs.get('pre')
- post = funcs.get('post')
- if pred is None:
- raise Exception("%r doesn't seem to be the right sort of function for a prompt handler", f)
-
- if pre is not None:
- register_pre_handler(pred, pre)
-
- if post is not None:
- register_post_handler(pred, post)
-
- return f
-
-
- def bool_type():
-
- def pred(o, d):
- return o is bool
-
-
- def pre(o, d):
- if d is True:
- return ('', '(Y/n)')
- elif d is False:
- return ('', '(y/N)')
- else:
- return ('', '(y/n)')
-
-
- def post(t, o, d):
- t = t.lower()
- if t in ('y', 'n'):
- return None if t == 'y' else False
-
- return d
-
- return locals()
-
- bool_type = register_prompt_handler(bool_type)
-
- def confirm_type():
-
- def pred(o, d):
- if o == 'confirm':
- pass
- return d is not None
-
-
- def pre(o, d):
- return ('', ': type "%s" (or "CANCEL" to cancel)' % d)
-
-
- def post(t, o, d):
- if t == d:
- return True
- elif t == 'CANCEL':
- return False
- else:
- return None
-
- return locals()
-
- confirm_type = register_prompt_handler(confirm_type)
-
- def str_type():
-
- def pred(o, d):
- return o is str
-
-
- def pre(o, d):
- return ('(default = %r)' % (d,), '')
-
-
- def post(t, o, d):
- if not t:
- pass
- return d
-
- return locals()
-
- str_type = register_prompt_handler(str_type)
-
- def strlist_type():
-
- def pred(o, d):
- return o is list
-
-
- def pre(o, d):
- return ('(default = %r)' % (d,), '(comma-separated)')
-
-
- def post(t, o, d):
- if t:
- return map(str.strip, t.strip(',').split(','))
- else:
- return d
-
- return locals()
-
- strlist_type = register_prompt_handler(strlist_type)
-
- def list_type():
-
- def pred(o, d):
- return type(o) is list
-
-
- def pre(o, d):
- options_str = '\n\t' + '\n\t'.join((lambda .0: for i, s in .0:
- '(%d) %s' % (i + 1, s))(enumerate(o)))
- default_str = '\n(default = %r)' % d
- return (default_str, options_str)
-
-
- def post(t, o, d):
- if not t:
- return d
-
-
- try:
- idx = int(t)
- except Exception:
- return None
-
- if idx == 0:
- return None
-
-
- try:
- return o[idx - 1]
- except IndexError:
- return None
-
-
- return locals()
-
- list_type = register_prompt_handler(list_type)
-
- def pre_prompt(prompt_str, options, default):
- pre_func = find_pre_function(options, default)
- if pre_func is None:
- raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
-
- (default_str, options_str) = pre_func(options, default)
- full_prompt_str = ('%s %s %s' % (prompt_str, options_str, default_str)).strip() + ': '
- return (full_prompt_str, options, default)
-
-
- def prompt(prompt_str, options = None, default = None, input_func = raw_input):
- (prompt_str, options, default) = pre_prompt(prompt_str, options, default)
- result = None
- while result is None:
-
- try:
- text = input_func(prompt_str).strip()
- except Exception:
- e = None
- raise e
- continue
-
- result = post_prompt(text, options, default)
- return result
-
-
- def find_match_for(pred_func_list, *args):
- for pred, func in pred_func_list:
- if pred(*args):
- return func
- continue
-
-
-
- def find_pre_function(options, default):
- return find_match_for(_pre_functions, options, default)
-
-
- def find_post_function(options, default):
- return find_match_for(_post_functions, options, default)
-
-
- def post_prompt(text, options, default):
- post_func = find_post_function(options, default)
- if post_func is None:
- raise NotImplementedError("Don't know what to do for options = %r, default = %r", options, default)
-
- return post_func(text, options, default)
-
-
- def _main():
- booltests = ((bool, True, [
- 'n'], False), (bool, False, [
- 'y'], True), (bool, None, [
- '',
- 'y'], True), (bool, True, [
- ''], True), (bool, False, [
- ''], False))
- confirmtests = (('confirm', 'b', [
- 'b'], True), ('confirm', 'b', [
- 'CANCEL'], False), ('confirm', 'b', [
- 'a',
- 'CANCEL'], False), ('confirm', 'b', [
- 'a',
- 'b'], True))
- strtests = ((str, 'a', [
- 'b'], 'b'), (str, 'a', [
- ''], 'a'), (str, None, [
- 'b'], 'b'), (str, None, [
- '',
- 'a'], 'a'))
- strlisttests = ((list('abcd'), 'a', [
- ''], 'a'), (list('abcd'), 'a', [
- ''], 'a'), (list('abcd'), 'a', [
- ''], 'a'), (list('abcd'), 'a', [
- ''], 'a'))
- listtests = ()
- for tests in (booltests, confirmtests, strtests, strlisttests, listtests):
- for opts, default, inputs, expected in tests:
-
- def input():
- yield None
- for input in inputs:
- yield input
-
- while True:
- yield ''
-
- input_gen = input()
- input_gen.next()
- result = prompt(repr(opts), opts, default, input_gen.send)
- input_gen.close()
- if result == expected:
- print ' O'
- continue
- print ' X (%r != %r)' % (result, expected)
-
-
-
- if __name__ == '__main__':
- _main()
-
-